<extend name="Public/layout"/>

<block name="body">
    <div class="row">
        <div class="form-horizontal" >
            <div class="col-xs-12">
                <div class="nav-tabs-custom flat">
                    <div class="tab-content no-padding">
                        <div class='tab-pane active' id="tab{$key}">
                            <div class="box-body" style="max-width:900px;">


                                <div class="form-group">
                                    <label class="col-sm-2 control-label" data-name="{$field['name']}">活动ID</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" name="act_id"
                                               value="{$activity['taobao_act_id']}" readonly="readonly">

                                        <input style="display: none" type="text" class="form-control"
                                               name="taobao_act_id"
                                               value="{$activity['taobao_act_id']}">
                                    </div>
                                </div>


                                <div class="form-group">
                                    <label class="col-sm-2 control-label" data-name="{$field['name']}">活动名称</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" name="act_id"
                                               value="{$activity['activity_title']}" readonly="readonly">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">开始时间</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" name=""
                                               value="{$activity['start_time']}" readonly="readonly">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="col-sm-2 control-label">结束时间</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" name=""
                                               value="{$activity['end_time']}" readonly="readonly">
                                    </div>
                                </div>


                                <div class="form-group">
                                    <label class="col-sm-2 control-label">产品链接</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" name="goods_url"
                                               value="">
                                        <button onclick="getGoodsDetail()">查询商品信息</button>
                                    </div>

                                </div>


                                <div class="box-body goods_div"
                                     style="display: none;max-width:900px;border-top-color: #3c8dbc; border-top-width: 3px; border-top-style: solid;">


                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">产品名称</label>
                                        <div class="col-sm-10">
                                            <input type="text" class="form-control" name="goodsName"
                                                   value="">
                                        </div>

                                    </div>




                                    <div class="goods_div" style="display: block">
                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">商品ID:</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="skuId"
                                                       value="" readonly="readonly">
                                            </div>
                                        </div>


                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">商品名称:</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="goodsName"
                                                       value="" readonly="readonly">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">商品图片:</label>
                                            <div class="col-sm-10">
                                                <img id="imgUrl" name="imgUrl" src="">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">商品分类:</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="cidName"
                                                       value="" readonly="readonly">
                                            </div>
                                        </div>



                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">商品单价:</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="unitPrice"
                                                       value="" readonly="readonly">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">商品落地页:</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="materialUrl"
                                                       value="" readonly="readonly">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">推广结束日期:</label>
                                            <div class="col-sm-10">

                                                <input type="hidden" name="endDate" class="text date"
                                                       value=""/>
                                                <input style="width: auto;" type="text" data-name="start_time"
                                                       class="form-control date datepicker" value="{:I('get.start_time')}"
                                                       placeholder="选择推广结束日期"
                                                       data-type="date"/>
                                            </div>
                                        </div>


                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">无线佣金比例:</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="commisionRatioWl"
                                                       value="" >
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">团长佣金比例:</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="tuanzhang_radio"
                                                       value="" >
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">优惠券链接:</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="coupon_url"
                                                       value="" >
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">券金额:</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="coupon_money"
                                                       value="" >
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">券后价:</label>
                                            <div class="col-sm-10">
                                                <input type="text" class="form-control" name="coupon_price"
                                                       value="" >
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">活动券图:</label>
                                            <div class="col-sm-10">

                                                    <p > <input type="hidden" id="filename1" name="filename1" value= ""/></p >
                                                    <p >上传文件：
                                                        <input type="file" onchange="doUpload()" id="file" name="kcs_uploader_file" accept="image/*"/></p>

                                            </div>
                                        </div>


                                        <div class="form-group">
                                            <label class="col-sm-2 control-label">结束券图片:</label>
                                            <div class="col-sm-10">
                                                <p > <input type="hidden" id="jieshuquan_img" name="jieshuquan_img" value= ""/></p >
                                                <p >上传文件：
                                                    <input type="file" onchange="doUpload2()" id="file2" name="kcs_uploader_file" accept="image/*"/></p>
                                            </div>
                                        </div>


                                    </div>


                                </div>
                                <div style="text-align: center;" class="form-group">
                                    <div class="col-sm-10">
                                        <button onclick="goodsSubmit()" style="color: #0a8ddf;width: 200px" type="submit">提交</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        $(function () {
            $('.form-control.date.datepicker').change(function () {
                $(this).prev().val($(this).val());
            });

            $('.datepicker').each(function () {
                var $this = $(this);
                var dateType = $(this).attr('data-type');
                var handle = "datepicker";
                var format = "yyyy-mm-dd";
                var options = {
                    language: 'zh-CN',
                    autoclose: true,
                    templates: {
                        leftArrow: '<i class="fa fa-caret-square-o-left"></i>',
                        rightArrow: '<i class="fa fa-long-arrow-right"></i>'
                    },
                }
                switch (dateType) {
                    case 'timestamp':
                    case 'datetime':
                        format = "yyyy-mm-dd hh:ii";
                        handle = 'datetimepicker';
                        break;
                    case 'date':
                        format = "yyyy-mm-dd";
                        handle = 'datepicker';
                        break;
                }
                options.format = format;
                var fieldName = $this.attr('data-name');
                $this[handle](options)
                        .on('changeDate,change', function (e) {
                            var value = $(e.currentTarget).val();
                            $('input[name=' + fieldName + ']').val(value);
                        });
            });
        });

        function getGoodsDetail() {
            var goods_url = $('input[name="goods_url"]').val();
            $.ajax({
                url: '{:U("activity/getGoodsDetail")}',
                type: 'post',
                data: {
                    goods_url: goods_url,
                },
                dataType: 'json',
                success: function (result) {
                    if(result.code != 200) alert(result.msg);
//                    $treeTable.addChilds($treeTable.build(result.data));
                    $(".goods_div").show();
//                    alert(result.data.goodsName);
                    $('input[name=goodsName]').val(result.data.goodsName);
                    $('input[name=skuId]').val(result.data.skuId);
                    $('input[name=cidName]').val(result.data.cidName);
                    $('input[name=cid2Name]').val(result.data.cid2Name);
                    $('input[name=cid3Name]').val(result.data.cid3Name);
                    $('input[name=unitPrice]').val(result.data.unitPrice);
                    $('input[name=materialUrl]').val(result.data.materialUrl);
                    $('input[name=endDate]').val(result.data.endDate);
                    $('input[name=commisionRatioWl]').val(result.data.commisionRatioWl);
                    $('input[name=commisionRatioPc]').val(result.data.commisionRatioPc);
                    $("#imgUrl").src = result.data.imgUrl;
                }
            });
        }

        function goodsSubmit(){
            //
            var taobao_act_id = "{$activity['taobao_act_id']}";
            var goods_url = $('input[name="goods_url"]').val();
            var tuanzhang_radio = $('input[name="tuanzhang_radio"]').val();
            var jieshuquan_img = $('input[name="jieshuquan_img"]').val();
            var filename1 = $('input[name="filename1"]').val();
            var endDate = $('input[name="endDate"]').val();
            var coupon_money = $('input[name="coupon_money"]').val();
            var coupon_price = $('input[name="coupon_price"]').val();

            $.ajax({
                url: '{:U("activity/addAct")}',
                type: 'post',
                data: {
                    goods_url: goods_url,
                    taobao_act_id: taobao_act_id,
                    tuanzhang_radio: tuanzhang_radio,
                    jieshuquan_img: jieshuquan_img,
                    filename1: filename1,
                    endDate: endDate,
                    coupon_money: coupon_money,
                    coupon_price: coupon_price
                },
                dataType: 'json',
                success: function (result) {
                    console.log('****');
                    console.log(result);

                        alert(result.msg);

//                    $treeTable.addChilds($treeTable.build(result.data));



                }
            });
        }

        function uploadFile(){
            var formData = new FormData($('#uploadForm')[0]);
            $.ajax({
                type: 'post',
                dataType: "json",
                url: "http://zs.jiuyuanmall.cn/api/Upload", //上传文件的请求路径必须是绝对路劲
                headers : {
                    'Content-Type' : 'application/x-www-form-urlencoded'
                },
                data: formData,
                cache: false,
                processData: false,
                contentType: false
            }).success(function (data) {
                alert(data);
            }).error(function () {
                alert("上传失败");
            });
        }

        function doUpload() {

            let data = new FormData();
            data.append('kcs_uploader_file', $('#file')[0].files[0]);
            let xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function(){
                if(xhr.readyState == 4 && xhr.status==200){
                    console.log(xhr.responseText);
                    console.log('####');

                    var data = JSON.parse(xhr.responseText).data;
                    var ecode = JSON.parse(xhr.responseText).error_code;

                    console.log(data)
                    if(ecode == 0){
                        document.getElementById('filename1').value = data.avatar_url;
                        console.log('####');
                        console.log(data.avatar_url)
                    }
                }
            };
            xhr.open('POST',( "http://"+location.hostname ) + '/api/Upload');
            xhr.send(data)
        }

        function doUpload2() {

            let data = new FormData();
            data.append('kcs_uploader_file', $('#file2')[0].files[0]);
            let xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function(){
                if(xhr.readyState == 4 && xhr.status==200){
                    console.log(xhr.responseText);
                    console.log('####');

                    var data = JSON.parse(xhr.responseText).data;
                    var ecode = JSON.parse(xhr.responseText).error_code;

                    console.log(data)
                    if(ecode == 0){
                        document.getElementById('jieshuquan_img').value = data.avatar_url;
                        console.log('####');
                        console.log(data.avatar_url)
                    }
                }
            };
            xhr.open('POST',( "http://"+location.hostname ) + '/api/Upload');
            xhr.send(data)
        }


        function sss() {
            var form = new FormData();
            form.file = file;    //file是从文件输入框的change事件中获取到的文件内容
            var uploadUrl = '/upload/images/';    //上传的路径
            try{
                var xhr;
                if(window.XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
                }else if(window.ActiveXObject) {
                    xhr = new ActiveXObject("Microsoft.XMLHttpRequest");
                }else {
                    console.warn('浏览器版本太低',err);
                    return;
                }

                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        const result = JSON.parse(xhr.responseText);
                        if (xhr.status === 200) {
                            // 上传成功
                            console.log(result);
                        } else {
                            // 上传失败
                        }
                    }
                };

                xhr.withCredentials = false;
                xhr.open("POST", uploadUrl, true);
                xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
                xhr.setRequestHeader("Content-Type","multipart/form-data");
                xhr.send(formData);
            }catch(err) {
                console.warn('错误：',err);
            }
        }

        /*function doUpload() {
            var formData = new FormData;
            formData.append('file', $('#file')[0].files[0]);
            console.log(8888888);
            console.log($('#file')[0].files[0]);
            $.ajax({
                // url: 'http://zs.sun.com/api/Upload' ,
                url: '{:U("activity/upload")}' ,
                /!*headers : {
                    'Content-Type' : "multipart/form-data"
                },*!/
                type: 'POST',
                data:formData,
                // async: false,
                // cache: false,
                processData: false,
                contentType: false,
                success: function (returndata) {
                    console.log(returndata);
                },
                error: function (returndata) {
                    console.log(returndata);
                }
            });
        }*/
    </script>
</block>